package com.xc.content.controller;

import com.google.common.base.Preconditions;
import com.xc.content.bo.CourseCategoryBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.CourseCategoryConverter;
import com.xc.content.dto.CourseCategoryDTO;
import com.xc.content.service.CourseCategoryService;
import com.xc.content.utils.ResponseResult;
import com.xc.content.utils.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-03
 * @Version: 1.0
 */
@RestController
@RequestMapping("/content/course")
@Api(value = "课程分类接口", tags = "课程分类接口")
@Slf4j
public class CourseCategoryController {
    @Resource
    private CourseCategoryService courseCategoryService;

    @ApiOperation(value = "新增分类接口")
    @PostMapping("/addCourseCategory")
    public ResponseResult<?> addCourseCategory(@RequestBody CourseCategoryDTO courseCategoryDTO) {
        Preconditions.checkNotNull(courseCategoryDTO.getName(), "分类名称不能为空");
        Preconditions.checkNotNull(courseCategoryDTO.getLabel(), "分类标签不能为空");
        Preconditions.checkNotNull(courseCategoryDTO.getParentId(), "父节点不能为空");
        Preconditions.checkNotNull(courseCategoryDTO.getIsShow(), "是否展示能为空");
        Preconditions.checkNotNull(courseCategoryDTO.getOrderBy(), "排序字段不能为空能为空");
        Preconditions.checkNotNull(courseCategoryDTO.getIsLeaf(), "叶子为能为空");

        CourseCategoryBO courseCategoryBO = CourseCategoryConverter.INSTANCE.convertDTOToBO(courseCategoryDTO);

        Boolean isAdd = courseCategoryService.addCourseCategory(courseCategoryBO);
        if (isAdd) {
            return ResponseResult.success("新增分类成功");
        } else {
            return ResponseResult.error(ExceptionCode.FIVE_HUNDRED_CODE,"新增分类失败");
        }
    }


    @ApiOperation(value = "树型查询分类接口")
    @GetMapping("/getCourseCategoryTree")
    public ResponseResult<?> getCourseCategoryTree() {
        List<TreeNode> courseCategoryTree = courseCategoryService.getCourseCategoryTree();
        if (CollectionUtils.isEmpty(courseCategoryTree)){
            return ResponseResult.error(ExceptionCode.FIVE_HUNDRED_CODE,"查询失败！");
        }
        return ResponseResult.success("查询成功!",courseCategoryTree);
    }


}
